<template>
  <div class="login-fullscreen">
    <div class="login-container" id="login-container">
      <div class="login-card">
        <div class="login-header">
          <h2 class="login-title">用户登录</h2>
        </div>
        <form @submit.prevent="handleLogin" class="login-form">
          <div class="form-group">
            <label for="username" class="form-label">账号</label>
            <input 
              type="text" 
              id="username" 
              v-model="loginForm.username" 
              required
              class="form-input"
              placeholder="请输入用户名"
            />
          </div>
          <div class="form-group">
            <label for="password" class="form-label">密码</label>
            <input 
              type="password" 
              id="password" 
              v-model="loginForm.password" 
              required
              class="form-input"
              placeholder="请输入密码"
            />
          </div>
          
          <!-- 错误信息显示 -->
          <div v-if="errorMessage" class="error-message">
            {{ errorMessage }}
          </div>
          
          <button type="submit" class="login-button" :disabled="isLoading">
            {{ isLoading ? '登录中...' : '登录' }}
          </button>
        </form>
        <div class="login-footer">
          <p class="back-home-link">
            <a @click="goToHome" class="link">返回首页</a>
          </p>
          <p class="register-link">
            还没有账号？<router-link to="/register" class="link">去注册</router-link>
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { authAPI } from '../api/auth.js'

export default {
  name: 'LoginView',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      isLoading: false,
      errorMessage: ''
    }
  },
  methods: {
    async handleLogin() {
      if (this.isLoading) return
      
      // 清除之前的错误信息
      this.errorMessage = ''
      
      // 基本验证
      if (!this.loginForm.username.trim()) {
        this.errorMessage = '请输入用户名'
        return
      }
      if (!this.loginForm.password.trim()) {
        this.errorMessage = '请输入密码'
        return
      }
      
      this.isLoading = true
      
      try {
        // 调用API服务层进行登录
        const result = await authAPI.login(this.loginForm.username, this.loginForm.password)
        
        if (result.success) {
          // 保存用户信息到localStorage
          localStorage.setItem('isLoggedIn', 'true')
          localStorage.setItem('username', result.user.username)
          localStorage.setItem('nickname', result.user.nickname || result.user.username)
          localStorage.setItem('token', result.token)
          
          // 登录成功后跳转到首页
          this.$router.push('/')
        } else {
          this.errorMessage = result.message || '登录失败'
        }
      } catch (error) {
        console.error('登录错误:', error)
        if (error.response) {
          // 服务器返回错误响应
          this.errorMessage = error.response.data.message || '登录失败，请检查用户名和密码'
        } else if (error.request) {
          // 网络错误
          this.errorMessage = '网络连接失败，请检查后端服务器是否运行'
        } else {
          // 其他错误
          this.errorMessage = '登录请求失败'
        }
      } finally {
        this.isLoading = false
      }
    },
    goToHome() {
      this.$router.push('/')
    }
  }
}
</script>

<style scoped>
html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  width: 100%;
  overflow: hidden;
}

* {
  box-sizing: border-box;
}

#app {
  margin: 0;
  padding: 0;
  height: 100vh;
  width: 100vw;
  overflow: hidden;
}
.login-fullscreen {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background-image: url('/images/login.png');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  z-index: 10000;
}

.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: transparent;
  padding: 0;
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.login-container::before {
  content: "";
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, rgba(255,255,255,0) 70%);
  transform: rotate(30deg);
  animation: float 20s infinite linear;
  z-index: 0;
}

.login-container::after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url("data:image/svg+xml,%3Csvg width='100' height='100' viewBox='0 0 100 100' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M11 18c3.866 0 7-3.134 7-7s-3.134-7-7-7-7 3.134-7 7 3.134 7 7 7zm48 25c3.866 0 7-3.134 7-7s-3.134-7-7-7-7 3.134-7 7 3.134 7 7 7zm-43-7c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zm63 31c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zM34 90c1.657 0 3-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm56-76c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zM12 86c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm28-65c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm23-11c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm-6 60c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm29 22c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zM32 63c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm57-13c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm-9-21c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM60 91c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM35 41c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM12 60c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2z' fill='%23ffffff' fill-opacity='0.05' fill-rule='evenodd'/%3E%3C/svg%3E");
  opacity: 0.3;
  z-index: 0;
}

@keyframes float {
  0% {
    transform: rotate(30deg) translate(0, 0);
  }
  25% {
    transform: rotate(30deg) translate(10px, 10px);
  }
  50% {
    transform: rotate(30deg) translate(0, 20px);
  }
  75% {
    transform: rotate(30deg) translate(-10px, 10px);
  }
  100% {
    transform: rotate(30deg) translate(0, 0);
  }
}

.login-card {
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(10px);
  border-radius: 20px;
  padding: 2.5rem;
  box-shadow: 0 8px 32px rgba(31, 38, 135, 0.37);
  border: 1px solid rgba(255, 255, 255, 0.18);
  width: 100%;
  max-width: 450px;
  transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  position: relative;
  z-index: 1;
  overflow: hidden;
}

.login-card::before {
  content: "";
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: linear-gradient(
    45deg,
    transparent,
    rgba(255, 255, 255, 0.2),
    transparent
  );
  transform: rotate(45deg);
  transition: all 0.6s ease;
  opacity: 0;
}

.login-card:hover {
  transform: translateY(-8px) scale(1.01);
  box-shadow: 0 12px 40px rgba(31, 38, 135, 0.5);
}

.login-card:hover::before {
  opacity: 1;
  animation: shine 1.5s ease;
}

@keyframes shine {
  0% {
    transform: translateX(-100%) translateY(-100%) rotate(45deg);
  }
  100% {
    transform: translateX(100%) translateY(100%) rotate(45deg);
  }
}

.login-header {
  text-align: center;
  margin-bottom: 2rem;
  position: relative;
}

.login-header::after {
  content: "";
  display: block;
  width: 50px;
  height: 3px;
  background: linear-gradient(90deg, #4CAF50, #66BB6A);
  margin: 0.8rem auto;
  border-radius: 3px;
}

.login-title {
  font-size: 2rem;
  font-weight: 700;
  color: #1f2937;
  margin: 0;
  background: linear-gradient(135deg, #4CAF50, #66BB6A);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.login-form {
  margin-bottom: 2rem;
}

.form-group {
  margin-bottom: 1.8rem;
  position: relative;
}

.form-label {
  display: block;
  font-weight: 600;
  color: #4b5563;
  margin-bottom: 0.7rem;
  font-size: 1.05rem;
  transition: all 0.3s;
}

.form-input {
  width: 100%;
  padding: 1.1rem 1.3rem;
  border: 2px solid #e5e7eb;
  border-radius: 12px;
  font-size: 1.1rem;
  transition: all 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55);
  box-sizing: border-box;
  background: rgba(249, 250, 251, 0.7);
  position: relative;
  z-index: 1;
}

.form-input:focus {
  outline: none;
  border-color: #4CAF50;
  box-shadow: 0 0 0 4px rgba(76, 175, 80, 0.2);
  transform: translateY(-2px);
  background: #fff;
}

.form-input:focus + .form-label,
.form-input:not(:placeholder-shown) + .form-label {
  transform: translateY(-25px);
  font-size: 0.9rem;
  color: #4CAF50;
}

.login-button {
  width: 100%;
  padding: 0.8rem 1.5rem;
  background: linear-gradient(135deg, #4CAF50, #66BB6A);
  color: #fff;
  border: none;
  border-radius: 20px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.4s cubic-bezier(0.23, 1, 0.320, 1);
  margin-top: 1rem;
  position: relative;
  overflow: hidden;
  z-index: 1;
  box-shadow: 0 4px 15px rgba(76, 175, 80, 0.3);
}

.login-button::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #4CAF50, #66BB6A);
  z-index: -1;
  transition: all 0.4s ease;
  opacity: 0;
}

.login-button:hover {
  transform: translateY(-3px);
  box-shadow: 0 8px 20px rgba(76, 175, 80, 0.5);
}

.login-button:hover::before {
  opacity: 1;
}

.login-button:active {
  transform: translateY(-1px);
}

.login-button:disabled {
  opacity: 0.7;
  cursor: not-allowed;
  transform: none;
}

.login-button:disabled:hover {
  transform: none;
  box-shadow: 0 4px 15px rgba(25, 118, 210, 0.3);
}

/* 错误信息样式 */
.error-message {
  background-color: #fee;
  border: 1px solid #fcc;
  color: #c33;
  padding: 0.8rem;
  border-radius: 8px;
  margin: 1rem 0;
  text-align: center;
  font-size: 0.9rem;
  animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.login-footer {
  text-align: center;
  margin-top: 2rem;
}

.register-link,
.back-home-link {
  margin: 0.7rem 0;
  color: #6b7280;
  font-size: 1.05rem;
  transition: all 0.3s;
}

.link {
  color: #4CAF50;
  cursor: pointer;
  text-decoration: none;
  font-weight: 600;
  transition: all 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
  padding: 0.3rem 0.5rem;
  border-radius: 6px;
  position: relative;
}

.link::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 50%;
  width: 0;
  height: 2px;
  background: #4CAF50;
  transition: all 0.3s ease;
  transform: translateX(-50%);
}

.link:hover {
  color: #4CAF50;
  text-decoration: none;
  transform: translateY(-2px);
}

.link:hover::after {
  width: 100%;
}

.link:active {
  transform: translateY(0);
}

@media (max-width: 768px) {
  .login-card {
    padding: 2rem 1.5rem;
    margin: 0.5rem;
    border-radius: 16px;
  }
  
  .login-title {
    font-size: 1.7rem;
  }
  
  .form-input {
    padding: 0.9rem 1.1rem;
    font-size: 1rem;
  }
  
  .login-button {
    padding: 0.7rem 1.3rem;
    font-size: 0.95rem;
    border-radius: 20px;
  }
  
  .register-link,
  .back-home-link {
    font-size: 1rem;
  }
}

@media (max-width: 480px) {
  .login-container {
    padding: 0.5rem;
  }
  
  .login-card {
    padding: 1.5rem 1rem;
  }
  
  .login-title {
    font-size: 1.5rem;
  }
  
  .form-input {
    padding: 0.8rem 1rem;
  }
  
  .login-button {
    padding: 0.6rem 1rem;
    font-size: 0.9rem;
    border-radius: 20px;
  }
}
</style>